package com.study.main.service;

import com.study.common.entity.User;
import com.study.common.pagination.Pagination;
import com.study.main.dao.MainDao;
import com.study.main.entity.Information;
import com.study.main.vo.ConditionPagination;
import com.study.main.vo.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Create by ldr.
 * @create 2018-08-30 14:37
 * @Description：逻辑层
 */
@Service(value = "mainServiceImpl")
public class MainServiceImpl implements MainService{

    @Autowired
    @Qualifier(value = "mainDao")
    private MainDao mainDao;

    @Override
    public User getById(int infoId) {
        return (User)mainDao.findById(User.class,infoId);
    }

    /**
     *  
     * @param cp 条件分页
     * @return com.study.common.pagination.Pagination
     *       
     */ 
    @Override
    public Pagination getPageByCondition(ConditionPagination cp) {
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("chinese_name",cp.getChineseName());
        map.put("telNumber",cp.getTelNumber());
        map.put("gitlabNumber",cp.getGitlabNumber());
        map.put("email",cp.getEmail());
        map.put("credit",cp.getCredit());
        map.put("grade",cp.getGrade());
        Long count = mainDao.getRows(map);
        List<UserInfo> userInfoList = mainDao.findPageByQuery(cp.getPageIndex(),cp.getLimit(),map);
        Pagination pagination = new Pagination();
        // 获取数据库数据的总行数
        pagination.setTotalRows(Integer.parseInt(count.toString()));
        // 将查询出的结果存入实体类中
        pagination.setList(userInfoList);
        return pagination;
    }

    @Override
    public void addInformation(Information info) {
        mainDao.add(info);
    }

    @Override
    public void deleteByIds(int[] ids) {
        mainDao.deleteByIds(ids);
    }

    @Override
    public void updateIdentity(int identityId,int infoId) {
        Information information = (Information)mainDao.findById(Information.class,infoId);
        mainDao.updateIdentity(identityId,information.getUserId());
    }
    /**
     * 修改用户的信息
     * @param userInfo  修改的信息
     * @return void
     *
     */
    @Override
    public void updateInfo(ConditionPagination userInfo) {
        Information information =  (Information)mainDao.findById(Information.class,Integer.parseInt(userInfo.getInfoId()));
        User user1 = (User) mainDao.findById(User.class,information.getUserId());
        User user = new User();
        Information info = new Information();
        info.setInfoId(Integer.parseInt(userInfo.getInfoId()));
        info.setUserId(information.getUserId());
        info.setGrade(Integer.parseInt(userInfo.getGrade()));
        info.setCredit(Integer.parseInt(userInfo.getCredit()));
        mainDao.updateInformation(info);
        user.setUserId(information.getUserId());
        user.setTelNumber(userInfo.getTelNumber());
        user.setChineseName(user1.getChineseName());
        user.setEmail(user1.getEmail());
        user.setGitlabNumber(userInfo.getGitlabNumber());
        user.setUserName(user1.getUserName());
        user.setIdentityId(user1.getIdentityId());
        mainDao.updateUser(user);
    }

    @Override
    public List<Information> getUserInfoListByUserId(int infoId) {
        return mainDao.findByUserId(infoId);
    }

    @Override
    public UserInfo getUserInfoById(int infoId) {;
        return mainDao.findByInfoId(infoId);
    }

    @Override
    public int getUserIdById(int i) {
        Information in = (Information)mainDao.findById(Information.class,i);
        int userId = in.getUserId() ;
        return userId;
    }

    @Override
    public boolean getPageByDis(int i) {
        Information in = (Information)mainDao.findById(Information.class,i);
        int userId = in.getUserId() ;
        if(mainDao.findByUserId(userId).size()>1){
            return true;
        }
        return false;
    }

    @Override
    public void updateInformation(int i) {
        Information information = new Information();
        information.setGrade(0);
        information.setCredit(0);
        information.setInfoId(i);
        mainDao.updateInformation(information);
    }

    @Override
    public Pagination getPage(ConditionPagination cp) {
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("chinese_name",cp.getChineseName());
        map.put("telNumber",cp.getTelNumber());
        map.put("gitlabNumber",cp.getGitlabNumber());
        map.put("email",cp.getEmail());
        map.put("credit",cp.getCredit());
        map.put("grade",cp.getGrade());
        Long count = mainDao.getRows(map);
        List<User> userInfoList = mainDao.findPageBy(map);
        Pagination pagination = new Pagination();
        // 获取数据库数据的总行数
        pagination.setTotalRows(Integer.parseInt(count.toString()));
        // 将查询出的结果存入实体类中
        pagination.setList(userInfoList);
        return pagination;
    }
}
